#include "wap_processbar.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_cursor.h"
#include "wa_label.h"
#include "wap.h"
#include "stdio.h"

static void wap_cursor_create(void);
static void wap_cursor_del(void);
static void wap_cursor_callback(wa_sign* sign);

wa_screen_obj wap_cursor_screen = 
{
	.id = WAP_WIN_CURSOR_ID,
	.create = wap_cursor_create,
	.del = wap_cursor_del,
};

wa_obj* wap_cursor_win;
wa_obj* wap_cursor_cursor;
wa_obj* wap_cursor_label;

extern wa_font font_song_16_ascii;

uint8_t wap_cursor_state;

void wap_cursor_init(void)
{
	wap_cursor_state = 0;
}

static void wap_cursor_create(void)
{
	wap_cursor_win = wa_win_create(NULL);
	wa_win_set_pos(wap_cursor_win, 0, 0);
	wa_win_set_size(wap_cursor_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_cursor_win, wa_nok);
	wa_win_set_callback(wap_cursor_win, wap_cursor_callback);
	wap_cursor_screen.win = wap_cursor_win;
	
	wap_cursor_label = wa_label_create(NULL);
	wa_win_add_obj(wap_cursor_win, wap_cursor_label);
	wa_obj_set_pos(wap_cursor_label, 0, 8);
	wa_obj_set_size(wap_cursor_label, WA_LCD_W, 16);
	wa_label_set_font(wap_cursor_label, &font_song_16_ascii);
	wa_label_set_text(wap_cursor_label, "cursor");
	wa_label_set_align_mode(wap_cursor_label, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
	
	wap_cursor_cursor = wa_cursor_create(NULL);
	wa_win_add_obj(wap_cursor_win, wap_cursor_cursor);
	wa_obj_set_pos(wap_cursor_cursor, 56, 32);
	wa_obj_set_size(wap_cursor_cursor, 16, 16);
	wa_cursor_set_type(wap_cursor_cursor, WA_CURSOR_TYPE_TRIA);
	wa_cursor_set_tria_type(wap_cursor_cursor, WA_CURSOR_TRIA_FILL);
	if(wap_cursor_state == 0)
	{
		wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_LEFT);
	}
	else if(wap_cursor_state == 1)
	{
		wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_RIGHT);
	}
	else if(wap_cursor_state == 2)
	{
		wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_UP);
	}
	else if(wap_cursor_state == 3)
	{
		wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_DOWN);
	}
}

static void wap_cursor_del(void)
{
	wa_obj_del(wap_cursor_win);
}

static void _wap_cursor_paint(wa_obj* obj)
{
}

static void wap_cursor_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_cursor_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			wap_cursor_state = 2;
			wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_UP);
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			wap_cursor_state = 3;
			wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_DOWN);
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			wap_cursor_state = 0;
			wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_LEFT);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			wap_cursor_state = 1;
			wa_cursor_set_tria_dir(wap_cursor_cursor, WA_CURSOR_TRIA_RIGHT);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}
